package me.zhengjie.inter.rest;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Qy;
import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.service.AdlXmxxService;
import me.zhengjie.business.service.HljgService;
import me.zhengjie.business.service.QyService;
import me.zhengjie.business.service.dto.QyDto;
import me.zhengjie.business.service.dto.QyQueryCriteria;
import me.zhengjie.business.service.dto.ZhsqQueryCriteria;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiahongbo
 * @date 2022-07-14
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "移动端签约管理")
@RequestMapping("/mp/qy")
public class MpQyController {

    private final QyService qyService;

    @GetMapping(value = "/choosedList")
    @ApiOperation("移动端查询选择机构")
    public ResponseEntity<Object> choosedList(QyQueryCriteria criteria) {
        List<QyDto> qyDtos = qyService.queryAll(criteria);
        return new ResponseEntity<>(PageUtil.toMpContent(qyService.queryAll(criteria)), HttpStatus.OK);
    }

    @GetMapping(value = "/incompleteList")
    @ApiOperation("移动端查询待签约")
    public ResponseEntity<Object> incompleteList(QyQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(PageUtil.toMpContent(qyService.queryIncompleteList(criteria.getBlurry(), pageable)), HttpStatus.OK);
    }

    @GetMapping(value = "/completedList")
    @ApiOperation("移动端查询已签约")
    public ResponseEntity<Object> completedList(QyQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(PageUtil.toMpContent(qyService.queryMpList(criteria, pageable)), HttpStatus.OK);
    }

    @PostMapping
    @Log("移动端新增签约")
    @ApiOperation("移动端新增签约")
    public ResponseEntity<Object> createQy(@Validated @RequestBody Qy resources) {
        JSONObject jsonObject = qyService.checkChooseHljg(resources.getBisQyZhsqId(), resources.getBisGsglCqhlfwlx());
        if("1".equals(jsonObject.getString("flag"))) {
            qyService.create(resources);
        }
        return new ResponseEntity<>(PageUtil.toMpContent(jsonObject), HttpStatus.OK);

    }
    @PostMapping("/sign")
    @Log("移动端执行签约")
    @ApiOperation("移动端执行签约")
    public ResponseEntity<Object> sign( @RequestBody Qy resources) {
        qyService.sign(resources);
        return new ResponseEntity<>(PageUtil.toMpContent(new HashMap<String,Object>()), HttpStatus.OK);
    }

    @GetMapping("/findByJgName")
    @Log("查询签约机构")
    @ApiOperation("查询签约机构")
    public ResponseEntity<Object> findByJgName(@RequestParam Long bisZhsqId) {
        String byZhId = qyService.findByZhId(bisZhsqId);
        return new ResponseEntity<>(PageUtil.toMpContent(byZhId), HttpStatus.OK);
    }

}
